package com.aote.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Element;

import com.browsesoft.baseadapter.AdvancedSimpleEntity;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLFormComponent;
import com.browsesoft.util.RequestHelper;

/**
 * 在非组件环境下执行SQL批处理
 */
public class BatchSQLQueryNotComponent extends BatchSQLQueryBase {
	/**
	 * 对应的组件和请求
	 */
	private HTMLBasicComponent component;

	private HttpServletRequest request;

	public BatchSQLQueryNotComponent(Element config,
			HttpServletRequest request, HTMLBasicComponent component) {
		this.component = component;
		this.request = request;
		this.setConfig(config);
	}

	/**
	 * 替换sql中的参数为父组件模型的属性值
	 */
	protected String replaceParams(String sql) {
		HTMLFormComponent comp = (HTMLFormComponent) ComponentHelper
				.getComponent(component, HTMLFormComponent.class);
		// 没有在form组件下，不替换
		if (comp != null) {
			AdvancedSimpleEntity entity = (AdvancedSimpleEntity) comp.getModel();
			Map attrs = entity.getAttributes();
			attrs = entity.formatDatas(attrs);
			sql = StringHelper.replaceStr(attrs, sql);
		}
		sql = RequestHelper.replaceParameter(request, sql);
		return sql;
	}
}
